package com.zmn.oms.dubbo.impl.normal.order;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.common.dio.normal.visit.OrderVisitQueryDIO;
import com.zmn.oms.common.dro.visit.OrderVisitDRO;
import com.zmn.oms.dubbo.interfaces.normal.order.OrderVisitRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.visit.OrderVisitDTO;
import com.zmn.oms.model.entity.visit.OrderVisitQuery;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkMultipleVisitBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.*;


/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2021/10/20 11:48
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class OrderVisitRemoteServiceImpl implements OrderVisitRemoteService {

    @Autowired
    private OrderWorkMultipleVisitBService orderWorkMultipleVisitBService;

    @Override
    public ResponseDTO<OrderVisitDRO> getOrderVisitByQuery(OrderVisitQueryDIO orderVisitQueryDIO) {

        log.info("查询单个上门入参：{}", JSON.toJSONString(orderVisitQueryDIO));
        try {
            OrderVisitQuery query = new OrderVisitQuery();
            query.setOrderId(orderVisitQueryDIO.getOrderId());
            query.setOrderPayId(orderVisitQueryDIO.getOrderPayId());
            query.setVisitId(orderVisitQueryDIO.getVisitId());
            OrderVisitDTO dto = orderWorkMultipleVisitBService.getOrderVisitByQuery(query);
            OrderVisitDRO dro = BeanMapper.map(dto, OrderVisitDRO.class);
            return ResponseDTO.success(dro);
        } catch (Exception e) {
            log.error("[{}]查询单个上门异常：[{}]", orderVisitQueryDIO.getOrderId(), e.getMessage());
            e.printStackTrace();
            return ResponseDTO.fail();
        }
    }

    @Override
    public ResponseDTO<List<OrderVisitDRO>> listOrderVisitByQuery(@NotNull(message = "dio不能为空") OrderVisitQueryDIO orderVisitQueryDIO) {
        log.info("查询多个上门入参：{}", JSON.toJSONString(orderVisitQueryDIO));
        try {
            List<OrderVisitDTO> orderVisitDTOList = orderWorkMultipleVisitBService.listOrderVisitByOrderIdAndMasterId(orderVisitQueryDIO.getOrderId(), orderVisitQueryDIO.getMasterId());
            List<OrderVisitDRO> orderVisitDROList = BeanMapper.mapList(orderVisitDTOList, OrderVisitDRO.class);
            return ResponseDTO.success(orderVisitDROList);
        } catch (Exception e) {
            log.error("[{}]查询单个上门异常：[{}]", orderVisitQueryDIO.getOrderId(), e.getMessage());
            e.printStackTrace();
            return ResponseDTO.fail();
        }
    }

}
